/**
 * @Author Puzzle
 * @Date 2022/6/22 9:49 上午
 **/

package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"zsc-boot/app/system_layer/model"
	"zsc-boot/app/system_layer/model/request"
	"zsc-boot/app/system_layer/service"
	"zsc-boot/core"
	"zsc-boot/core/web"
)

var (
	ServiceMenuApi     = new(serviceMenuApi)
	serviceMenuService = service.ServiceMenuService
)

type serviceMenuApi struct {
}

// CreateServiceMenu 创建菜单
func (s *serviceMenuApi) CreateServiceMenu(ctx *gin.Context) {
	var serviceMenu model.ServiceMenu
	_ = ctx.BindJSON(&serviceMenu)
	if err := serviceMenuService.CreateMenu(serviceMenu); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// DeleteServiceMenu 删除菜单
func (s *serviceMenuApi) DeleteServiceMenu(ctx *gin.Context) {
	var serviceMenu model.ServiceMenu
	_ = ctx.BindJSON(&serviceMenu)
	if err := serviceMenuService.DeleteServiceMenu(serviceMenu); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// UpdateServiceMenu 更新菜单
func (s *serviceMenuApi) UpdateServiceMenu(ctx *gin.Context) {
	var serviceMenu model.ServiceMenu
	_ = ctx.BindJSON(&serviceMenu)
	if err := serviceMenuService.UpdateServiceMenu(serviceMenu); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// UpdateServiceMenuStatus 更新菜单状态
func (s *serviceMenuApi) UpdateServiceMenuStatus(ctx *gin.Context) {
	var serviceMenu model.ServiceMenu
	_ = ctx.BindJSON(&serviceMenu)
	if err := serviceMenuService.UpdateServiceMenuStatus(serviceMenu); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// GetServiceMenu 查询菜单详情
func (s *serviceMenuApi) GetServiceMenu(ctx *gin.Context) {
	var id core.GetById
	_ = ctx.BindQuery(&id)
	if err, data := serviceMenuService.GetServiceMenu(id.ID); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, data)
	}
}

// GetServiceMenuList 查询菜单列表
func (s *serviceMenuApi) GetServiceMenuList(ctx *gin.Context) {
	var pageInfo request.ServiceMenuSearch
	_ = ctx.BindQuery(&pageInfo)
	if err, list, total := serviceMenuService.GetServiceMenuList(pageInfo); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessListResponse(ctx, list, total, pageInfo.Page, pageInfo.PageSize)
	}
}
